package cn.com.jslh.boarpro.main.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.main.MainDetilActivity;
import cn.com.jslh.boarpro.main.presenter.MenuPresenterImpl;
import cn.com.jslh.boarpro.main.view.view.MenuFragmentView;
import cn.com.jslh.boarpro.util.ModuleUtil;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class MenuFragment extends BaseFragment implements MenuFragmentView{
    @BindView(R.id.menuList)
    RecyclerView menuList;

    private MenuPresenterImpl menuPresenter;

    public MenuFragment() {
        // Required empty public constructor
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        menuPresenter = new MenuPresenterImpl(getContext(),this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_menu, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        menuList.setLayoutManager(linearLayoutManager);
        menuList.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        menuPresenter.setUI(menuList);
        return view;
    }

    /**
     * 接受主页发送来的调整menu内容的通知
     * @param moduleUtil
     */
    @Subscribe
    public void getModuleUtil(ModuleUtil moduleUtil) {
        menuPresenter.upDatas(moduleUtil.getModuleName());
    }

    /**
     * 发送menu更改通知
     * @param moduleUtil
     */
    public static void setModuleUtil(final ModuleUtil moduleUtil) {
        Flowable.just("1")
                .map(new Function<String, String>() {
                    @Override
                    public String apply(@NonNull String s) throws Exception {
                        Thread.sleep(1000);//延迟1秒加载
                        return "1";
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        EventBus.getDefault().post(moduleUtil);
                    }
                });
    }

    @Override
    public void onItemClicked(String... params) {
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onMenuClickListener(ModuleUtil moduleUtil) {
        MainDetilActivity.setMainModule(moduleUtil); //关联标题到主页
    }
}
